package web.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间处理工具类
 */
public class DateUtil {
    /**
     * 获取当前时间以Date形式返回
     * @return
     */
	public static Date getNowToDate(){
		return new Date();
	}

    /**
     * 获取当前时间以int形式返回
     * @return
     */
	public static int getNowToInt(){
		return (int)(getNowToDate().getTime()/1000);
	}
	public static int getNowToIntDate(){
		
		return getTodayToInt();
	}

	/**
	 * 获取当前时间的字符串  ： 通过给定的时间格式
	 * @param sdfStr 给定格式
	 * @return
	 */
	public static String getNowDateStr(String sdfStr){
		SimpleDateFormat sdf = new SimpleDateFormat(sdfStr);
		Date date=new Date();
		String dateStr=sdf.format(date);
		return dateStr;
	}

    public static int getIntDateToString(String time){

        int t=0;
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            t= (int)(dateFormat.parse(time).getTime()/1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }catch (Exception e){

        }
        return t;
    }
    public static String getStringDate(int time){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long millions=new Long(time).longValue()*1000;
        Date date=new Date(millions);
        return sdf.format(date);
    }

    public static String getStringDate(int time,String fromt){
        SimpleDateFormat sdf = new SimpleDateFormat(fromt);
        long millions=new Long(time).longValue()*1000;
        Date date=new Date(millions);
        return sdf.format(date);
    }
	public static int getTodayToInt(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String time = sdf.format(new Date());
		Date date = null;
		try {
			 date= sdf.parse(time);
		} catch (ParseException e) {
//			e.printStackTrace();
		}
		return (int)(date.getTime()/1000);
	}
	public static String getDateToStr(Date date){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		return time;
	}
    public static String getDateToString(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(date);
        return time;
    }
	public static int getDateToInt(Date date){
		return (int)(date.getTime()/1000);
	}
	public static int getStringToInt(String pattern,String date){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Date d; 
		try {
			d= sdf.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException("DateUtil.getStringToInt() has a error.");
		}
		return (int)(d.getTime()/1000);
	}
	
	public static String getDateToStr(String pattern,Date date){
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	public static int  getMaketTimeB(int i){
//        Calendar calendar= Calendar.getInstance();
//        calendar.add(Calendar.MONTH,-(i+1));
//        return calendar.get(Calendar.SECOND);
        return  DateUtil.getNowToInt()-(i+1)*3600*24*30;
    }
	public static int getMaketTimeS(int i){
//        Calendar calendar=Calendar.getInstance();
//        calendar.add(Calendar.MONTH,-i);
//        return calendar.get(Calendar.SECOND);
        return DateUtil.getNowToInt()-i*3600*24*30;
    }

//	截取s时间年月日部分
	public static int getTimeByDay(int time){
		String timeStr=getStringDate(time,"yyyy-MM-dd");
		return getIntDateToString(timeStr);
	}


	/**
	 * 获取时间int(10位) : 通过给定的dateStr 和时间格式
	 * @param dateStr 时间字符串
	 * @param sdfStr 时间字符格式
	 * @return
	 */
	public static int getIntByDateStr(String dateStr,String sdfStr){
		if(dateStr==null ||"".equals(dateStr)){
			return 0;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(sdfStr);
		Date date=null;
		try {
			date=sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long l=date.getTime();
		int i=(int)(l/1000);
		return i;
	}

    public static void main(String[]args){
//        System.out.println(getMaketTimeB(11));
        System.out.println(getStringDate(1476516433,"yyyy-MM-dd HH:mm:ss"));

    }
}
